package com.fan.shop.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.shop.domain.StockReq;
import com.fan.shop.domain.StockReqLine;

@Named("stockReqDao")
public class StockReqDaoImpl implements StockReqDao {
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public StockReq getStockReq(String requisitionNo) {
		Session session = sessionFactory.getCurrentSession();
		StockReq stockReq = (StockReq) session.get(StockReq.class,
				requisitionNo);
		return stockReq;
	}

	@SuppressWarnings("unchecked")
	public List<StockReq> getAllStockReqs() {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(StockReq.class);
		List<StockReq> list = criteria.list();
		return list;
	}

	public int getSize() {
		return getAllStockReqs().size();
	}

	public void addStockReq(StockReq stockReq) {
		Session session = sessionFactory.getCurrentSession();
		session.save(stockReq);
	}

	public void updateStockReq(StockReq stockReq) {
		Session session = sessionFactory.getCurrentSession();
		session.update(stockReq);
	}

	public void deleteStockReq(StockReq stockReq) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(stockReq);
	}

	public boolean hasStockReq(String requisitionNo) {
		if (getStockReq(requisitionNo) == null) {
			return false;
		} else {
			return true;
		}
	}

	public List<String> getColumnNames() {
		List<String> columnNames = new ArrayList<String>();
		columnNames.add("Requisition No");
		columnNames.add("Store Code");
		columnNames.add("Officer Code");
		columnNames.add("Transaction Code");
		columnNames.add("Transaction Date");
		columnNames.add("Received Date");
		columnNames.add("Postage");
		columnNames.add("Status");
		columnNames.add("Comment");

		return columnNames;
	}

	public BigDecimal getTotalPrice(String requisitionNo) {
		double totalPrice = 0.0;

		for (StockReqLine stockReqLine : getStockReqLines(requisitionNo)) {
			BigDecimal price = stockReqLine.getPrice();
			int qty = stockReqLine.getQty();
			totalPrice += price.doubleValue() * qty;
			System.out.println(totalPrice);
		}
		return new BigDecimal(totalPrice);
	}

	public int getTotalQty(String requisitionNo) {
		int totalQty = 0;
		for (StockReqLine stockReqLine : getStockReqLines(requisitionNo)) {
			int qty = stockReqLine.getQty();
			totalQty += qty;
		}
		return totalQty;
	}

	@SuppressWarnings("unchecked")
	public List<StockReqLine> getStockReqLines(String requisitionNo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(StockReqLine.class);
		criteria.add(Restrictions.eq("id.requisitionNo", requisitionNo));
		List<StockReqLine> lines = criteria.list();
		return lines;
	}

}
